<template>
  <div class="login-container">
    <div class="login-form">
      <div class="form-user" @keypress="loginPress">
        <div class="login-text">
          <div>
            <div>山西省化二建医院({{ $ts("账号登录") }})</div>
            <div class="login-line"></div>
          </div>
          <div style="flex: 1"></div>
        </div>
        <div class="login-text-small">欢迎登录山西省化二建医院信息系统</div>
        <div class="item">
          <div class="input-icon el-icon-user"></div>
          <input
            type="text"
            v-focus
            v-model="userInfo.userName"
            :placeholder="$ts(['请输入', '账号'])"
          />
        </div>
        <div class="item">
          <div class="input-icon el-icon-lock"></div>
          <input
            type="password"
            v-focus
            v-model="userInfo.password"
            :placeholder="$ts(['请输入', '密码'])"
          />
        </div>
        <div class="item">
          <div class="input-icon el-icon-mobile"></div>
          <input
            v-focus
            type="text"
            v-model="userInfo.verificationCode"
            :placeholder="$ts(['请输入', '验证码'])"
          />
          <div class="code" @click="getVierificationCode">
            <img v-show="codeImgSrc != ''" :src="codeImgSrc" />
          </div>
        </div>
      </div>
      <div class="loging-btn">
        <el-button
          size="large"
          :loading="loading"
          color="#2b6eac"
          :dark="true"
          @click="login"
          long
        >
          <span v-if="!loading">{{ $ts("登录") }}</span>
          <span v-else>{{ $ts("正在登录") }}...</span>
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, reactive, toRefs, getCurrentInstance } from "vue";
import { useRouter, useRoute } from "vue-router";
import store from "../store/index";
import http from "@/../src/api/http.js";
export default defineComponent({
  setup(props, context) {
    const loading = ref(false);
    const codeImgSrc = ref("");
    const userInfo = reactive({
      userName: "",
      password: "",
      verificationCode: "",
      UUID: undefined,
    });

    const getVierificationCode = () => {
      http.get("/api/User/getVierificationCode").then((x) => {
        codeImgSrc.value = "data:image/png;base64," + x.img;
        userInfo.UUID = x.uuid;
      });
    };
    getVierificationCode();
    let router = useRouter();
    const { proxy } = getCurrentInstance();
    const { $message, $ts } = proxy;
    const login = () => {
      if (!userInfo.userName) return $message.error($ts(["请输入", "账号"]));
      if (!userInfo.password) return $message.error($ts(["请输入", "密码"]));
      if (!userInfo.verificationCode) {
        return $message.error($ts(["请输入", "验证码"]));
      }
      loading.value = true;
      http.post("/api/user/login", userInfo, $ts("正在登录") + "....").then((result) => {
        if (!result.status) {
          loading.value = false;
          getVierificationCode();
          return $message.error(result.message);
        }
        store.commit("setUserInfo", result.data);
        router.push({ path: "/" });
      });
    };
    const loginPress = (e) => {
      if (e.keyCode == 13) {
        login();
      }
    };
    return {
      loading,
      codeImgSrc,
      getVierificationCode,
      login,
      userInfo,
      loginPress,
    };
  },
  directives: {
    focus: {
      inserted: function (el) {
        el.focus();
      },
    },
  },
});
</script>
<style lang="less" scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100%;
  background: url('@/img/yiyuan.webp') center center/cover no-repeat, linear-gradient(
    to left,
    #e6f0faCC,
    #f5fafdCC,
    #e6f0faCC
  );
  justify-content: center;
  align-items: center;
}

.login-form {
  align-items: center;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  z-index: 999;
  background: rgba(255, 255, 255, 0.25); /* 半透明白色 */
  backdrop-filter: blur(16px) saturate(180%);
  -webkit-backdrop-filter: blur(16px) saturate(180%);
  padding: 35px 25px;
  box-shadow: 2px 4px 28px #b3d1ea66;
  border-radius: 10px;
  border: 1.5px solid #2b6eac33;
  .form-user {
    .item {
      border-radius: 5px;
      border: 1px solid #c7e0f7;
      display: flex;
      margin-bottom: 30px;
      background: #fafdff;
      height: 45px;
      padding-left: 20px;
      display: flex;
      .code {
        position: relative;
        cursor: pointer;
        width: 74px;
        padding: 5px 10px 0 0;
      }
      .input-icon {
        line-height: 45px;
        color: #2b6eac;
        padding-right: 20px;
        font-size: 18px;
      }
    }
  }
  input:-webkit-autofill {
    box-shadow: 0 0 0px 1000px white inset;
    -webkit-box-shadow: 0 0 0px 1000px white inset !important;
  }
  input {
    background: white;
    display: block;
    box-sizing: border-box;
    width: 100%;
    min-width: 0;
    margin: 0;
    padding: 0;
    color: #323233;
    line-height: inherit;
    text-align: left;
    border: 0;
    outline: none;
    font-size: 16px;
    line-height: 20px;
  }
}

.form-user,
.loging-btn {
  width: 100%;
}

.loging-btn {
  box-shadow: 2px 4px 11px #a4c2ff33;
  margin-top: 2px;
  margin-bottom: 10px;
  button {
    padding: 21px;
    font-size: 15px !important;
    width: 100%;
    background: linear-gradient(90deg, #2b6eac 0%, #5bb6e9 100%);
    border: none;
    color: #fff;
    font-weight: bold;
    letter-spacing: 2px;
  }
}

.login-text {
  font-weight: bolder;
  font-size: 22px;
  letter-spacing: 2px;
  color: #2b6eac;
  position: relative;
  display: flex;
  .login-line {
    z-index: -1;
    padding: 5px;
    position: relative;
    top: -8px;
    width: 100%;
    background: none;
  }
}

.login-text-small {
  margin-bottom: 20px;
  font-size: 19px;
  color: #eaeff0;
  text-align: center;
  font-weight: 500;
}
</style>
